<?php defined('SYSPATH') or die('No direct script access.');
/**
 *
 * 采购付款单批量上传
 * @author locove
 *
 */
class Controller_Smtsales extends Controller
{
    /**
     * 描述 : ebay销量统计
     * 作者 : Edgar.lee
     */
    public function action_indexs() {
        if( PHP_SAPI !== 'cli' ) return;

        set_time_limit(0);                                                                                              //防超时
        ignore_user_abort(true);

        //定义时令时间差
        $bet = 15.5;//小时
        $sec = $bet * 60 * 60;
        $count = 0;                                                                                                     //导入计数
        $page = 1;                                                                                                      //当前分页
        $maxDate = date('Y-m-d', $_SERVER['REQUEST_TIME']);
        $nowDate = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
        echo "交期导入开始 : {$nowDate} \n";

        $sql = 'SELECT
            DATE_FORMAT(`add_time`, "%Y-%m-%d") maxDate
        FROM
            `smt_sales`
        ORDER BY
            `id` DESC 
        LIMIT 1';
        $minDate = DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();                                    //读取数据库最大时间
        $minDate = empty($minDate) ? date('Y-m-d', $_SERVER['REQUEST_TIME'] - 2592000) : $minDate[0]['maxDate'];        //生成最小范围的过滤条件

        $minDate_sec = strtotime($minDate);
        $maxDate_sec = strtotime($maxDate);
        $sql = "SELECT
            `productinfo`.skuCode sku, 
            SUM(`productinfo`.productCount) c, 
            DATE_FORMAT(`orderinfo`.gmtPayTime + INTERVAL $sec SECOND, '%Y-%m-%d') `date`
        FROM
            `orderinfo`
                LEFT JOIN `productinfo` ON
                    `productinfo`.orderId = `orderinfo`.orderId
        WHERE
            UNIX_TIMESTAMP(`orderinfo`.gmtPayTime) + {$sec} >= '{$minDate_sec}'
        AND UNIX_TIMESTAMP(`orderinfo`.gmtPayTime) + {$sec} < '{$maxDate_sec}'
        AND `productinfo`.skuCode <> ''
        /*AND `orderinfo`.orderstatus = '23'*/
        GROUP BY
            `productinfo`.skuCode, DATE_FORMAT(`orderinfo`.gmtPayTime + INTERVAL $sec SECOND, '%Y-%m-%d')";

        while( stagerpc::getSmtSysSkuSales($sql . ' LIMIT ' . ($page++ - 1) * 100 . ', 100', $data) ) {
            foreach($data as &$v) {
                $v = '("' . join('","', tool::slashesDeep($v)) . "\", \"{$nowDate}\")";
                $count += 1;
            }
            $data = 'INSERT INTO `smt_sales` (
                `sku`, `sale`, `count_date`, `add_time`
            ) VALUES ' . join(',', $data);
            DB::query(Database::INSERT, $data)->execute('newerp');
        }

        checkstock::bomb('smt',$maxDate);
        checkstock::badBegin('smt');
        checkstock::activeOut(2, $minDate);
        $nowDate = time() - $_SERVER['REQUEST_TIME'];
        echo "交期导入结束 : 导入 {$count} 条, 耗时 {$nowDate} 秒\n";
    }

    public function action_index() {
        if( PHP_SAPI !== 'cli' ) return;

        set_time_limit(0);                                                                                              //防超时
        ignore_user_abort(true);

        //开始添加字段
        $this->updatePayTimeCn();

        $count = 0;                                                                                                     //导入计数
        $page = 1;                                                                                                      //当前分页
        $maxDate = date('Y-m-d', $_SERVER['REQUEST_TIME']);
        $nowDate = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
        echo "交期导入开始 : {$nowDate} \n";
        $sql = 'SELECT
            DATE_FORMAT(`add_time`, "%Y-%m-%d") maxDate
        FROM
            `smt_sales`
        ORDER BY
            `id` DESC
        LIMIT 1';
        $minDate = DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();                                    //读取数据库最大时间
        $minDate = empty($minDate) ? date('Y-m-d', $_SERVER['REQUEST_TIME'] - 2592000) : $minDate[0]['maxDate'];        //生成最小范围的过滤条件

        $minDate_sec = strtotime($minDate);
        $maxDate_sec = strtotime($maxDate);
        $sql = "SELECT
            `productinfo`.skuCode sku,
            SUM(`productinfo`.productCount) c,
            FROM_UNIXTIME(`orderinfo`.payTimeCn, '%Y-%m-%d') `date`
        FROM
            `orderinfo`
                LEFT JOIN `productinfo` ON
                    `productinfo`.orderId = `orderinfo`.orderId
        WHERE
            `orderinfo`.payTimeCn >= '{$minDate_sec}'
        AND `orderinfo`.payTimeCn < '{$maxDate_sec}'
        AND `productinfo`.skuCode <> ''
        GROUP BY
            `productinfo`.skuCode, FROM_UNIXTIME(`orderinfo`.payTimeCn, '%Y-%m-%d')";

        while( stagerpc::getSmtSysSkuSales($sql . ' LIMIT ' . ($page++ - 1) * 500 . ', 500', $data) ) {
            foreach($data as &$v) {
                $v = '("' . join('","', tool::slashesDeep($v)) . "\", \"{$nowDate}\")";
                $count += 1;
            }
            $data = 'INSERT INTO `smt_sales` (
                `sku`, `sale`, `count_date`, `add_time`
            ) VALUES ' . join(',', $data);
            DB::query(Database::INSERT, $data)->execute('newerp');
        }

        checkstock::bomb('smt', $minDate);
        checkstock::expSum('smt');
        $nowDate = time() - $_SERVER['REQUEST_TIME'];
        echo "交期导入结束 : 导入 {$count} 条, 耗时 {$nowDate} 秒\n";
    }

    public function updatePayTimeCn () {
        //冬令时为11月8日至次年3月11日
        $md = strtotime(date("1970-m-d"));

        $sec = 5932800 < $md && $md < 26841600 ? 15 * 60 * 60 : 16 * 60 * 60;

        $sql = "UPDATE
             `orderinfo`
        SET
             `payTimeCn` = UNIX_TIMESTAMP(`gmtPayTime`) + {$sec}
        WHERE
             `payTimeCn` = 0
        AND
             `gmtPayTime` <> '0000-00-00 00:00:00'";

        stagerpc::addColumn ($sql);
    }
}